package top.shen33.yang.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.shen33.yang.base.R;
import top.shen33.yang.model.entity.QrtzJobDetails;
import top.shen33.yang.model.vo.QuartzEntity;
import top.shen33.yang.services.QrtzJobDetailsService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/job")
public class JobController {
	private final static Logger LOGGER = LoggerFactory.getLogger(JobController.class);
	

	@Autowired
    private Scheduler scheduler;

    @Autowired
    private QrtzJobDetailsService jobService;
    
	@GetMapping("/get/{sid}")
	public Object getOne(@PathVariable String sid){

		LOGGER.info("查看详情");
		QuartzEntity quartzEntity = jobService.getOneQuartzEntity(sid);
		return R.ok(quartzEntity);
	}


	@PostMapping("/save")
	public Object save(@RequestBody QuartzEntity quartz){
		LOGGER.info("新增任务");
		try {
	        //如果是修改  展示旧的 任务
	        if(quartz.getOldJobGroup()!=null){
	        	JobKey key = new JobKey(quartz.getOldJobName(),quartz.getOldJobGroup());
	        	scheduler.deleteJob(key);
	        }
	        Class cls = Class.forName(quartz.getJobClassName()) ;
	        cls.newInstance();

	        //构建JobDetail
	        JobDetail job = JobBuilder.newJob(cls).withIdentity(quartz.getJobName(),
	        		quartz.getJobGroup())
	        		.withDescription(quartz.getDescription()).build();


	        // 触发时间点
	        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getCronExpression());
	        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger"+quartz.getJobName(), quartz.getJobGroup())
	                .startNow().withSchedule(cronScheduleBuilder).build();

	        //交由Scheduler安排触发
	        scheduler.scheduleJob(job, trigger);

		} catch (Exception e) {
			e.printStackTrace();
			return R.err("新增任务error");
		}
		return R.ok();
	}


	@GetMapping("/list")
	public Object list(QuartzEntity quartz, Page page){
		LOGGER.info("任务列表");
		List<QuartzEntity> list = jobService.listQuartzEntity(quartz, page);
		page.setRecords(list);
		return R.ok(page);
	}


	@PostMapping("/trigger")
	public  Object trigger(QuartzEntity quartz,HttpServletResponse response) {
		LOGGER.info("触发任务");
		try {
		     JobKey key = new JobKey(quartz.getJobName(),quartz.getJobGroup());
		     scheduler.triggerJob(key);
		} catch (SchedulerException e) {
			 e.printStackTrace();
			 return R.err("触发任务失败");
		}

		return R.ok();

	}


	@PostMapping("/pause")
	public  Object pause(QuartzEntity quartz,HttpServletResponse response) {
		LOGGER.info("停止任务");
		try {
		     JobKey key = new JobKey(quartz.getJobName(),quartz.getJobGroup());
		     scheduler.pauseJob(key);
		} catch (SchedulerException e) {
			 e.printStackTrace();
			 return R.err("停止任务失败");
		}
		return R.ok();
	}


	@PostMapping("/resume")
	public  Object resume(QuartzEntity quartz,HttpServletResponse response) {
		LOGGER.info("恢复任务");
		try {
		     JobKey key = new JobKey(quartz.getJobName(),quartz.getJobGroup());
		     scheduler.resumeJob(key);
		} catch (SchedulerException e) {
			 e.printStackTrace();
			return R.err("恢复任务失败");
		}
		return R.ok();
	}



	@PostMapping("/remove")
	public  Object remove(QuartzEntity quartz,HttpServletResponse response) {
		LOGGER.info("移除任务");
		try {  
            TriggerKey triggerKey = TriggerKey.triggerKey(quartz.getJobName(), quartz.getJobGroup());  
            // 停止触发器  
            scheduler.pauseTrigger(triggerKey);  
            // 移除触发器  
            scheduler.unscheduleJob(triggerKey);  
            // 删除任务  
            scheduler.deleteJob(JobKey.jobKey(quartz.getJobName(), quartz.getJobGroup()));  
            System.out.println("removeJob:"+JobKey.jobKey(quartz.getJobName()));  
        } catch (Exception e) {  
        	e.printStackTrace();
			return R.err("移除任务失败");
        }  
		return R.ok();
	}
}
